package _5_multithread._1_basic;


/**
 * Created by bpzj on 6/24/2018.
 */
public class JoinMain {
    public volatile static int i = 0;

    public static class AddThread extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程执行完");
        }

        public AddThread(String name) {
            super(name);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        AddThread at = new AddThread("testJoinThread");
        at.start();
        // 使用join方法，表示 main线程会等待 at 线程执行
        // at.join();
        // 相当于
        synchronized (at) {
            while (at.isAlive()) {
                at.wait();
            }
        }
        // join()方法返回时，at线程已经执行完，所以 i 总是 100000000
        System.out.println("主线程执行");
    }

    /* join() 的本质：
    while (isAlive()) {
        long delay = millis - now;
        if (delay <= 0) {
            break;
        }
        wait(delay);
        now = System.currentTimeMillis() - base;
    }
    */

    /*
    yield 的用法

     */

}
